/**
 * Copyright (C), 2024, xxxx有限公司
 * FileName: VerifyUtils
 * Author:   TLVM
 * Date:     3/25/2024 1:41 PM
 * Description: 校验Utils
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.iqcctt.businessprocess.utils;

import com.iqcctt.businessprocess.annotation.BPE;
import com.iqcctt.businessprocess.execption.BusinessProcessException;

import java.lang.reflect.Field;

/**
 * 〈一句话功能简述〉<br>
 * 〈校验Utils〉
 *
 * @author TLVM
 * @create 3/25/2024
 * @since 1.0.0
 */
public class VerifyUtils {
    /*
     * @Author LiTao
     * @Description 校验长度是否一致
     * @Date 1:42 PM 3/25/2024
     * @Param 校验长度是否一致
     * @return
     **/
    public static void verifyFieldLen(Object result, BPE bpe){
        if(bpe.condiField().length!=bpe.fieldValue().length){
            throw new BusinessProcessException("condiField长度与fieldValue不一致!");
        }
    }

    /*
     * @Author LiTao
     * @Description 校验控制参数不一致,返回true
     * @Date 1:59 PM 3/25/2024
     * @Param
     * @param result 实体参数
     * @param bpe 注解控制参数
     * @return
     **/
    public static Boolean verifyContinue(Object result, BPE bpe) {
        Boolean isCondField = true;
        for (int i = 0; i < bpe.condiField().length; i++) {
            if(isCondField&&!(String.valueOf(getValue(result, bpe.condiField()[i])).equals(bpe.fieldValue()[i]))){
                isCondField = false;
            }
        }
        if(!isCondField) {
            return true;
        }
        return false;
    }

    public static Object getValue(Object object, String fieldName) {
        try {

            Field field = object.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(object);
        } catch (NoSuchFieldException e) {

        } catch (IllegalAccessException e) {

        }
        return null;
    }
}
